<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Customer extends Authenticatable implements JWTSubject
{
    use Notifiable;



    const GUARD_NAME = 'admin';
    protected $table = 'customers';
    //隐藏的字段
    protected $hidden=['email', 'admin', 'updated_at'];
    // 受保护的字段
    protected $guards=[];

    protected $appends=['amount'];

    /**
     * Get the identifier that will be stored in the subject claim of the JWT.
     *
     * @return mixed
     */
    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Return a key value array, containing any custom claims to be added to the JWT.
     *
     * @return array
     */
    public function getJWTCustomClaims()
    {
        return [];
    }

    public function apikeys()
    {
        return $this->hasmany('App\Models\ApiKey');
    }

    public function charges()
    {
        return $this->hasmany('App\Models\Charge');
    }

    public static function findByAreaCodePhone(string $area, string $phone)
    {
        return (new self)->where(compact('area','phone'))->first();
    }

    public function getAmountAttribute()
    {
        return ($this->charges()->latest()->value('change_after'))/100.0;
    }

}
